package com.wx.activitys.drp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.sd4324530.fastweixin.api.entity.UserInfo;
import com.wx.activitys.drp.EnumUtils.EnumTream;
import com.wx.activitys.drp.EnumUtils.GradeState;
import com.wx.activitys.drp.mapper.BGradeMapper;
import com.wx.activitys.drp.mapper.BUserInfoMapper;
import com.wx.activitys.drp.mapper.ProfitMapper;
import com.wx.activitys.drp.mapper.ShareMapper;
import com.wx.activitys.drp.mapper.UserInfoMapper;
import com.wx.activitys.drp.model.BGrade;
import com.wx.activitys.drp.model.BProfit;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.BUserInfoService;


@Service("/BUserInfoService")
public class BUserInfoServiceImpl implements BUserInfoService {
    @Autowired
    BUserInfoMapper bUserInfoMapper;
    @Autowired
    ProfitMapper profitMapper;
    @Autowired
    ShareMapper shareMapper;
    @Autowired
    BGradeMapper bGradeMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

	@Override
	public List<BUserInfo> queryPageList(BUserInfo entity) {
		
		return bUserInfoMapper.queryPageList(entity);
	}

	@Override
	public int selectCount(BUserInfo entity) {
		// TODO Auto-generated method stub
		return bUserInfoMapper.selectCount(entity);
	}

	@Override
	public int insert(BUserInfo entity) {
		// TODO Auto-generated method stub
		//entity.setId(DigitalSignature.getUUID());
		entity.setCreateTime(new Date());
		return bUserInfoMapper.insert(entity);
	}

	@Override
	public int update(BUserInfo entity) {
		// TODO Auto-generated method stub
	 List<BProfit> pr= profitMapper.findByRoleKey(entity.getRoleKey());
	 Map<String,Object> map = new HashMap<>();
	 for (BProfit bProfit : pr) {
		map.clear();
		map.put("openid", entity.getOpenid());
		map.put("platfromid",bProfit.getPaltformId());
		map.put("defaultValue", bProfit.getValue());
		shareMapper.update(map);
	}
		return bUserInfoMapper.update(entity);
	}

	@Override
	public BUserInfo selectEntity(BUserInfo entity) {
		// TODO Auto-generated method stub
		return bUserInfoMapper.selectEntity(entity);
	}

	@Override
	public int delete(BUserInfo entity) {
		// TODO Auto-generated method stub
		return bUserInfoMapper.delete(entity);
	}

	@Override
	public int batchupdate(String roleKey, String[] ids) {
		BUserInfo entity = new BUserInfo();
		entity.setRoleKey(roleKey);
		entity.getMap().put("ids", ids);
		int flag=bUserInfoMapper.update(entity);
		Map<String,Object> map = new HashMap<>();
		List<BProfit> pr= profitMapper.findByRoleKey(entity.getRoleKey());
		 for (BProfit bProfit : pr) {
			 for (int i = 0; i < ids.length; i++) {
				    map.clear();
					map.put("openid", ids[i]);
					map.put("platfromid",bProfit.getPaltformId());
					map.put("defaultValue", bProfit.getValue());
					flag=shareMapper.update(map);
			}
		}
		 return flag;
	}

	@Override
	public List<BUserInfo> queryTopTenList(BUserInfo entity) {
		return bUserInfoMapper.queryTopTenList(entity);
	}

	@Override
	public List<BUserInfo> selectByEntity(BUserInfo entity) {
		return bUserInfoMapper.selectByEntity(entity);
	}

	@Override
	public List<BUserInfo> queryPageRegistList(BUserInfo entity) {
		return bUserInfoMapper.queryPageRegistList(entity);
	}

	@Override
	public int count(BUserInfo entity) {
		return bUserInfoMapper.count(entity);
	}

	@Override
	public int updateState(BUserInfo userInfo, String gradeId) {
		BUserInfo userInfo1=userInfoMapper.findByOpenid(userInfo.getOpenid());
		//经销商升级，tream等级变成TOP，如果是分销商1级则下属人员自动升为分销商1级
		if(userInfo1.getTreamRole().equals(EnumTream.分销商1级.getValue())){
			BUserInfo entity = new BUserInfo();
			entity.setpId(userInfo.getOpenid());
			List<BUserInfo> list=bUserInfoMapper.selectByEntity(entity);
			if(list.size()>0){
				List<String> ids = new ArrayList<>();
				BUserInfo entity1 = new BUserInfo();
				for (BUserInfo bUserInfo : list) {
					ids.add(bUserInfo.getOpenid());
				}
				entity1.setTreamRole(EnumTream.分销商1级.getValue());
				entity1.getMap().put("ids", ids);
				bUserInfoMapper.update(entity1);
			}
		}
		userInfo.setTreamRole(EnumTream.经销商.getValue());
		int result =bUserInfoMapper.update(userInfo);
		if(StringUtils.isNotBlank(gradeId)){
			BGrade bGrade=new BGrade();
			bGrade.setId(gradeId);
			bGrade.setState(GradeState.通过.getValue());
			result=bGradeMapper.update(bGrade);
		}
		return result;
	}
}
